package Campaign;
use KIT_DB;

sub new 
{
    my ($class)	= @_;
    my ($self)	= {};

    $self->{campaign_id}	= 0;
    $self->{content_id}	= 0;
    $self->{group_id}	= 0;
    $self->{sender_email}	= '';
    $self->{sender_name}	= '';
    $self->{realm_id}	= 0;
    $self->{exit_link}	= '';

    bless $self, 'Campaign';
    return $self;
}

sub newFromDB
{
    my ($class, $campaign_id)	= @_;
    my $self	= {};

    my $stmt	= "SELECT id, content_id, group_id, sender_name, sender_email, use_salutation, realm_id, campaign_date, exit_link FROM campaigns WHERE id = $campaign_id";

    my $kit_db  = new KIT_DB();
    my $hashref	= $kit_db->GetSingleRow($stmt);

    $self->{campaign_id}    = $hashref->{id};
    $self->{content_id}     = $hashref->{content_id};
    $self->{group_id}       = $hashref->{group_id};
    $self->{use_salutation} = $hashref->{use_salutation};
    $self->{sender_name}    = $hashref->{sender_name};
    $self->{sender_email}   = $hashref->{sender_email};
    $self->{realm_id}       = $hashref->{realm_id};
    $self->{campaign_date}  = $hashref->{campaign_date};
    $self->{exit_link}      = $hashref->{exit_link};

    bless $self, 'Campaign';
    return $self;
}

sub GetCampaignId
{
    my ($self, $group_id, $content_id)	= @_;

    my $kit_db  = new KIT_DB();
    return $kit_db->GetSingleValue("SELECT id AS ret_val FROM campaigns WHERE group_id = $group_id AND content_id = $content_id");
}

sub InsertCampaign
{
    my $self	= shift;
    my $sender_email= $self->{sender_email};

    my $stmt	= "INSERT INTO campaigns (content_id, group_id, sender_email, sender_name, realm_id, exit_link) 
                   VALUES (
                        $self->{content_id}, $self->{group_id}, '$self->{sender_email}', 
                        '$self->{sender_name}', $self->{realm_id}, '$self->{exit_link}')";

    my $kit_db  = new KIT_DB();
    return $kit_db->Insert($stmt);
}

sub UpdateCampaign
{
    my $self	= shift;

    my $sender_email    = $self->{sender_email};
    my $sender_name     = $self->{sender_name};
    my $exit_link       = $self->{exit_link};

    my $stmt	= "UPDATE campaigns 
                   SET  content_id = $self->{content_id}, group_id = $self->{group_id}, sender_email = '$sender_email', 
                        sender_name = '$sender_name', exit_link = '$exit_link'
                   WHERE id = $self->{campaign_id}";

    my $kit_db  = new KIT_DB();
    return $kit_db->Update($stmt);
}

sub GetClickCount
{
    my $self	= shift;
    my $stmt	= "SELECT count(*) AS ret_val FROM clicks WHERE campaign_id = " . $self->{campaign_id};

    my $kit_db  = new KIT_DB();
    return $kit_db->GetSingleValue($stmt);
}

sub GetViewCount
{
    my $self    = shift;
    my $stmt    = "SELECT count(*) AS ret_val FROM views WHERE campaign_id = " . $self->{campaign_id}; # . " AND count(*) > $min_view_count";

    my $kit_db  = new KIT_DB();
    return $kit_db->GetSingleValue($stmt);
}

sub GetUnsubscribeCount
{
    my $self	= shift;
    my $stmt	= "SELECT count(*) AS ret_val FROM unsubscribes WHERE campaign_id = " . $self->{campaign_id};

    my $kit_db  = new KIT_DB();
    return $kit_db->GetSingleValue($stmt);
}

sub GetAllCampaigns
{
    my $self	= shift;

    my $stmt	= "SELECT   C.id, C.group_id, C.content_id, C.sender_name, C.sender_email, C.campaign_date 
                   FROM     campaigns AS C, groups AS G
                   WHERE    C.realm_id = $self->{realm_id}
                   AND      G.active = 1
                   AND      C.group_id = G.id
                   ORDER BY C.id DESC";

    my $kit_db  = new KIT_DB();
    my @rows	= $kit_db->GetMultipleRows($stmt);

    my @campaigns	=();

    foreach (@rows) 
    {
        my $campaign_id	= $_->[0];
        my $group_id	= $_->[1];
        my $content_id	= $_->[2];
        my $campaign_date= $_->[5];

        my $group	= newFromDB Groups($group_id);
            $addr_count	= $group->GetRecipientCount();

            $addr_count	= 1 if ($addr_count == 0);

        my $content	= newFromDB Content($content_id);

        $self->{campaign_id}	= $campaign_id;

        my $view_count	= $self->GetViewCount();
        my $click_count	= $self->GetClickCount();
        my $unsubsc_count = $self->GetUnsubscribeCount();

        my $click_count_pc	= sprintf("%.2f", ($click_count / $addr_count)*100);
        my $view_count_pc	= sprintf("%.2f", ($view_count / $addr_count)*100);
        my $unsubsc_count_pc	= sprintf("%.2f", ($unsubsc_count / $addr_count)*100);

        my %campaign	= (
                campaign_id	=> $campaign_id,

                group_id	=> $_->[1],
                group_name	=> $group->{group_name},
                active_count	=> $addr_count,

                content_id	=> $_->[2],
                content_title	=> $content->{title},

                sender_name	=> $_->[3],
                sender_email	=> $_->[4],

                campaign_date	=> $campaign_date,

                click_count	=> $click_count,
                view_count	=> $view_count,
                unsubsc_count	=> $unsubsc_count,

                click_count_pc	=> $click_count_pc,
                view_count_pc	=> $view_count_pc,
                unsubsc_count_pc=> $unsubsc_count_pc,
        );
        push @campaigns, \%campaign;
    }
    return @campaigns;
}

1;